<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="培训人员" prop="trainerNametxt">
        <el-input
          v-model="queryParams.trainerNametxt"
          placeholder="请输入培训人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="departmentName">

        <el-select
              v-model="queryParams.departmentName"
              placeholder="请选择部门"  style="width:100%"
            >
              <el-option v-for="item in deptlist" :key="item.deptId" :value="item.deptId" :label="item.deptName"/>
            </el-select>
      </el-form-item>
      <el-form-item label="课程名称" prop="trainingCourseName">
        <el-input
          v-model="queryParams.trainingCourseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hrmanagement:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hrmanagement:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hrmanagement:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hrmanagement:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="培训记录ID" align="center" prop="id" /> -->
      <el-table-column label="课程名称" align="center" prop="trainingCourseName" />
      <el-table-column label="培训人员" align="center" prop="trainerNametxt"  width="230"/>
      <el-table-column label="所属部门" align="center" prop="departmentNameTxt" />
      <el-table-column label="培训渠道" align="center" prop="trainingChannel" />
      <el-table-column label="培训类别" align="center" prop="trainingCategory" />
      <el-table-column label="培训时间" align="center" prop="trainingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.trainingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="培训地点" align="center" prop="trainingLocation" />
      <el-table-column label="培训机构" align="center" prop="trainingOrganizationName" />
      <el-table-column label="机构联系人" align="center" prop="trainingOrganizationContact" />


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" min-width="125">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hrmanagement:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hrmanagement:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改培训信息记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="培训人员" prop="trainerName">


          <UserSelect ref="userselect"    :open="userOpen" @cancel="userOpen=false" :type="'multiple'" @submit="submituser"></UserSelect>
<el-select v-model="form.trainerName" ref="elSelect" class="hide-popper" multiple  placeholder="请选择" @focus="useropen"  >
    <el-option
    placeholder="请选择招聘联系人"
      v-for="item in useroptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
        </el-form-item>
        <el-form-item label="所属部门" prop="departmentName">



          <el-select
              v-model="form.departmentName"
              placeholder="请选择部门"  style="width:100%"
            >
              <el-option v-for="item in deptlist" :key="item.deptId" :value="item.deptId" :label="item.deptName"/>
            </el-select>
        </el-form-item>
     <el-row>
      <el-col :span="12">
        <el-form-item label="培训渠道" prop="trainingChannel">
          <el-select
                  v-model="form.trainingChannel"
                  placeholder="请选择培训渠道"
                >
                  <el-option
                    v-for="dict in dict.type.hrm_dev_pxfs"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                    placeholder="请选择培训渠道"
                  ></el-option>
                </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="培训类别" prop="trainingCategory">

<el-select
        v-model="form.trainingCategory"
        placeholder="请选择培训类别"
      >
        <el-option
          v-for="dict in dict.type.hrm_dev_pxlb"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
          placeholder="请选择培训类别"
        ></el-option>
      </el-select>



</el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="培训时间" prop="trainingDate">
          <el-date-picker clearable
            v-model="form.trainingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择培训时间">
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      
        <el-form-item label="培训地点" prop="trainingLocation">
          <el-input v-model="form.trainingLocation" placeholder="请输入培训地点" />
        </el-form-item>
      </el-col>
     </el-row>
        <el-form-item label="培训机构名称" prop="trainingOrganizationName">
          <el-input v-model="form.trainingOrganizationName" placeholder="请输入培训机构名称" />
        </el-form-item>
        <el-form-item label="培训机构联系人" prop="trainingOrganizationContact">
          <el-input v-model="form.trainingOrganizationContact" placeholder="请输入培训机构联系人" />
        </el-form-item>
        <el-form-item label="培训课程名称" prop="trainingCourseName">
          <el-input v-model="form.trainingCourseName" placeholder="请输入培训课程名称" />
        </el-form-item>
        <el-form-item
                label="培训文件"
                label-width="130px"
                prop="zuiGaoXueLiXueXiFangShi"
              >
                <el-upload
                  ref="upload"
                  :limit="10"
                  :action="upload.url"
                  :headers="upload.headers"
                  :file-list="upload.fileList"
                  :on-progress="handleFileUploadProgress"
                   :on-success="handleFileSuccess"
                  :auto-upload="true"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                 :before-remove="beforeRemove"
                >
                  <el-button slot="trigger" size="small" type="primary"
                    >选取文件</el-button
                  >

                </el-upload>

              </el-form-item>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/hrmanagement/info";
import { getUserSelect } from "@/api/system/user";
import UserSelect from "@/components/PeopleSelect/index";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { AllDept } from "@/api/system/dept";
import { getToken } from "@/utils/auth";
import {getfilelist} from "@/api/system/info";
export default {
  dicts: [
  "hrm_dev_pxfs",
  "hrm_dev_pxlb"

  ],
  name: "Info",
  components: { Treeselect ,UserSelect},
  data() {
    return {
      upload: {
        isUploading: false,
        headers: { Authorization: "Bearer " + getToken() },
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        fileList: [],
      },
      deptlist:[],
      useroptions:[],
      userOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训信息记录表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      nickNameList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainerName: null,
        departmentNameTxt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        trainerName: [
          { required: true, message: "培训人员不能为空", trigger: "blur" }
        ],
        departmentNameTxt: [
          { required: true, message: "所属部门不能为空", trigger: "blur" }
        ],
        trainingChannel: [
          { required: true, message: "培训渠道不能为空", trigger: "blur" }
        ],
        trainingCategory: [
          { required: true, message: "培训类别不能为空", trigger: "blur" }
        ],
        trainingDate: [
          { required: true, message: "培训时间不能为空", trigger: "blur" }
        ],
        trainingLocation: [
          { required: true, message: "培训地点不能为空", trigger: "blur" }
        ],
        trainingOrganizationName: [
          { required: true, message: "培训机构名称不能为空", trigger: "blur" }
        ],
        trainingOrganizationContact: [
          { required: true, message: "培训机构联系人不能为空", trigger: "blur" }
        ],
        trainingCourseName: [
          { required: true, message: "培训课程名称不能为空", trigger: "blur" }
        ],
        // departmentName: [
        //   { required: true, message: "所属部门不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
    AllDept().then(response => {
          if (response.code === 200) {
            this.deptlist= response.data
          } else {
            this.$message.error(response.message)
          }
        })
    getUserSelect().then(response => {
this.useroptions = response.data

      });
 

  },
  methods: {
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
       // 文件上传成功处理
       handleFileSuccess(response, file, fileList) {
      if (response.code === 200) {
        this.upload.fileList.push({name: response.originalFilename ,filePath:response.filePath});

      } 
    },
    handlePreview(file) {
console.log("file");
window.location.href ="http://localhost:8085"+file.filePath

},
handleExceed(files, fileList) {
this.$message.warning(
  `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
    files.length + fileList.length
  } 个文件`
);
},
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${file.name}？`);
},
    useropen(){
      console.log("打开选人组件");
        this.userOpen=true;
        this.$refs.elSelect.blur()

       },
    submituser(peopleList,nickNameList){
      this.form.trainerName =peopleList
this.nickNameList =nickNameList
console.log("sdfddddddddddddddddddddddddddd");
   console.log(this.nickNameList);   
 this.$forceUpdate()
        this.userOpen=false;
      },
    /** 查询培训信息记录列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainerName: null,
        departmentNameTxt: null,
        trainingChannel: null,
        trainingCategory: null,
        trainingDate: null,
        trainingLocation: null,
        trainingOrganizationName: null,
        trainingOrganizationContact: null,
        trainingCourseName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        departmentName: null,
        trainerNametxt:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训信息记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.form.trainerName = this.form.trainerName.split(",")
    

        this.form.trainerName = this.form.trainerName.map(item => {return +item;}); 
        this.open = true;
        this.title = "修改培训信息记录";
      });
      getfilelist(id,"OaTrainingInfo").then((response) => {
      this.upload.fileList = response.data

    
      });
    },
    /** 提交按钮 */
    submitForm() {

     var OBJ=this.deptlist.find(item => item.deptId === this.form.departmentName)
     console.log("dsaff");
     console.log(OBJ);
     if (OBJ!==undefined && OBJ!==null && OBJ!=="") {
      this.form.departmentNameTxt = OBJ.deptName
     }
 
       this.form.trainerName=this.form.trainerName.join(",") 
      this.form.trainerNametxt = this.nickNameList.join(",")
     
      var commonDto = {
  form:this.form,
  Extra: JSON.stringify(this.upload.fileList)
}
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(commonDto).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(commonDto).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训信息记录编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hrmanagement/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
